package com.hnkywl.sts.util;

import net.ruixin.orm.hibernate.Page;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-4-23
 * Time: 上午11:26
 * To change this template use File | Settings | File Templates.
 */
public class EasyuiAdapter {
    /**
     * 返回分页dataGrid要求的json数据
     *
     * @param pages
     * @param propertyNames 需要转换的列
     * @return dataGrid要求的json数据
     */
    public static String getJsonForDataGrid(Page pages, String... propertyNames) {
        return getJsonForDataGrid(pages.getResult(),pages.getTotalCount(),propertyNames);
    }

    /**
     * 返回dataGrid要求的json数据
     * @param list 当前列表
     * @param total 列表所有的行总数
     * @param propertyNames 需要转换的属性字段
     * @return dataGrid要求的json数据
     */
    public static String getJsonForDataGrid(List list,int total, String... propertyNames) {
        String json = getJsonByList(list, propertyNames);
        return "{\"total\":" + total + ",\"rows\":[" + json + "]}";
    }

    public static String getJsonForCombo(List list,String... propertyNames) {
        String json = getJsonByList(list,propertyNames);
        return "[" + json + "]";
    }

    /**
     * 返回要求的Json
     *
     * @param list
     * @param propertyNames
     * @return 返回要求的Json
     */
    public static String getJsonByList(List list, String... propertyNames) {
        return BaseUtils.fetchCollectionToJson(list, propertyNames);
    }
}
